package com.youkeda.meetingroom.dao.mapper;

import com.youkeda.meetingroom.dao.dataobject.MrMeetingRoomPassword;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultType;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

@Mapper

public interface MrMeetingRoomPasswordMapper {

    /**
     * 通过userid删除整条记录
     *
     * @param meetingRoomId userid
     * @return 是否成功
     */
    @Delete({
            "delete from mr_meeting_room_password",
            "where meeting_room_id = #{meetingRoomId,jdbcType=VARCHAR}"
    })
    int deleteByMeetingRoomId(String meetingRoomId);


    @Insert({
            "insert into mr_meeting_room_password (id, gmt_created, ",
            "gmt_modified, pwd, ",
            "salt, meeting_room_id, ",
            "user_id, valid, due_time)",
            "values (#{id,jdbcType=VARCHAR}, #{gmtCreated,jdbcType=TIMESTAMP}, ",
            "#{gmtModified,jdbcType=TIMESTAMP}, #{pwd,jdbcType=VARCHAR}, ",
            "#{salt,jdbcType=VARCHAR}, #{meetingRoomId,jdbcType=VARCHAR}, ",
            "#{userId,jdbcType=VARCHAR}, #{valid,jdbcType=BIT}, #{dueTime,jdbcType=TIMESTAMP})"
    })
    int insert(MrMeetingRoomPassword record);


    /**
     * 通过userid查询明文密码,再取出明文密码,并进行比对
     *
     * @param meetingRoomId 主键id
     * @return 会议室明文密码
     */
    @Select({
            "select",
            "pwd",
            "from mr_meeting_room_password",
            "where meeting_room_id = #{meetingRoomId,jdbcType=VARCHAR}"
    })
    @Results({
            @Result(column = "pwd", property = "pwd", jdbcType = JdbcType.VARCHAR),
    })
    MrMeetingRoomPassword selectPwdByUserId(String meetingRoomId);


    @Update({
            "update mr_meeting_room_password",
            "set gmt_created = #{gmtCreated,jdbcType=TIMESTAMP},",
            "gmt_modified = #{gmtModified,jdbcType=TIMESTAMP},",
            "pwd = #{pwd,jdbcType=VARCHAR},",
            "salt = #{salt,jdbcType=VARCHAR},",
            "meeting_room_id = #{meetingRoomId,jdbcType=VARCHAR},",
            "user_id = #{userId,jdbcType=VARCHAR},",
            "valid = #{valid,jdbcType=BIT},",
            "due_time = #{dueTime,jdbcType=TIMESTAMP}",
            "where id = #{id,jdbcType=VARCHAR}"
    })
    int updateByPrimaryKey(MrMeetingRoomPassword record);


    /**
     * 通过会议室id修改密码内容和密码修改时间
     *
     * @param meetingRoomId 会议室id
     * @return 是否修改成功
     */
    @Update({
            "update mr_meeting_room_password",
            "gmt_modified = #{gmtModified,jdbcType=TIMESTAMP},",
            "pwd = #{pwd,jdbcType=VARCHAR},",
            "where meeting_room_id = #{meetingRoomId,jdbcType=VARCHAR}"
    })
    int updateByMeetingRoomId(String meetingRoomId);



    /**
     * 通过userid查询记录数量
     * @param userId userid
     * @return 记录数量
     */
    @Select({
            "select",
            "count(*)",
            "from mr_meeting_room_password",
            "where user_id = #{userId,jdbcType=VARCHAR}"
    })
    @ResultType(Integer.class)
    int selectCountByUserId(String userId);

}